package com.example.store2.Controller;


import com.example.store2.Service.ResourcesService;
import com.example.store2.pojo.PageBean;
import com.example.store2.pojo.Resources;
import com.example.store2.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/resources")
public class ResourcesController {
    @Autowired
    private ResourcesService resourcesService;

    /**
     * 分页查询个人资源
     * @param page
     * @param pageSize
     * 用户id id
     * @param classesId
     * @param resourcesName
     * @return
     */
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,Integer id,Integer classesId,String resourcesName){
        log.info("当前用户所使用的id:{},资源类型id:{},资源名:{}",id,classesId,resourcesName);
        PageBean pageBean=resourcesService.list(page,pageSize,id,classesId,resourcesName);
        return Result.success(pageBean);
    }

    /**
     * 添加资源
     * @param resources
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Resources resources){
        log.info("添加的资源为{}",resources);
        if(resourcesService.add(resources))
            return Result.success();
        else
            return Result.error("添加已达到上限，请升级等级");
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除id:{}",id);
        resourcesService.delete(id);
        return Result.success();
    }

    /**
     * 修改资源
     * @param resources
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Resources resources){
        log.info("修改的资源为:{}",resources);
        resourcesService.update(resources);
        return Result.success();
    }
}
